package com.wus.common.feign;

import cn.hutool.json.JSONUtil;
import com.wus.common.core.account.AccountThreadLocal;
import com.wus.common.core.constant.AccountConstants;
import com.wus.common.core.domain.user.AccountInfo;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description: feign调用传参设置
 * @Author: wus
 * @Date: 2024/12/17 12:01
 */
public class FeignHeaderInterceptor implements RequestInterceptor {

    private final Logger logger = LoggerFactory.getLogger(FeignHeaderInterceptor.class);

    @Override
    public void apply(RequestTemplate template) {
        logger.info("......进入feign调用拦截器");
        AccountInfo accountInfo = AccountThreadLocal.get();
        if (accountInfo != null) {
            // 给下游服务设置用户下上文信息
            logger.info("feign调用，设置用户上下文信息请求头accountInfo={}，避免重复查库耗时...", accountInfo);
            template.header(AccountConstants.ACCOUNT_INFO_CONTEXT_HEADER, JSONUtil.toJsonStr(accountInfo));
        }
        // 可以添加更多的header
    }
}
